/**
 * @Name: DrawIcon
 * @Description:
 * @Author: colfish
 * @Date: 2021/4/19
 */
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;

import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

public class DrawIcon implements Icon{        //实现 Icon 接口

    private int width;        //声明图标的宽
    private int height;        //声明图标的长

    public DrawIcon(int width,int height) {        //定义构造方法
        this.width=width;
        this.height=height;
    }
	
    @Override
    public int getIconHeight() {
        return this.height;
    }
 
    @Override
    public int getIconWidth() {
        return this.width;
    }
 
    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x, y, width, height);        //绘制一个圆形
    }
	
    public static void main(String[] args) {
        DrawIcon icon = new DrawIcon(15,15);
        //创建一个标签，并设置标签上的文字在标签正中间
        JLabel jLabel = new JLabel("测试", icon, SwingConstants.CENTER);
        JFrame jFrame = new JFrame();        //创建一个 JFrame 窗口
        Container container = jFrame.getContentPane();
        container.add(jLabel);
        jFrame.setSize(100,100);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
}